<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Define Variables
//-----------------------------------------------------------------
if(!isset($_GET['d'])) $_GET['d'] = NULL;
if(!isset($_GET['error'])) $_GET['error'] = NULL;
$selectBox = NULL;
$selected = NULL;

switch($a){
	//-----------------------------------------------------------------
	//  View All Pages
	//-----------------------------------------------------------------	
	case 1:
	
		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }

		//-----------------------------------------------------------------
		//  Get pages from DB
		//-----------------------------------------------------------------
		$pages = $db->getTable("SELECT * FROM page");
		
		//-----------------------------------------------------------------
		//  Create Layout
		//-----------------------------------------------------------------
		
		if($pages != FALSE){
			foreach($pages as$idx=> $page){
			/* Access Colours */
			if($page['type'] == "admin") {
				$fcolor = "red";
			}elseif($page['type'] == "registered") {
				$fcolor = "orange";
			}elseif($page['type'] == "all") {
				$fcolor = "black";
			}else{
				$fcolor = "black";
			}
			
			/* The Layout */
			$layout .= "<tr class='con1'>";
			$layout .= "<td>{$page['id']}</td>";
			$layout .= "<td><a href='$PHP_SELF?n=pages/pages&d={$page['shortname']}'>{$page['name']}</a></td>";
			$layout .= "<td><a href='http://localhost/jupiter/index.php?n=modules/users&a=1&d={$page['user_id']}'>{$page['user_name']}</a></td>";
			$layout .= "<td><font color='$fcolor'>{$page['type']}</font></td>";
			$layout .= "<td>".today_yesterday($page['date'])."</td>";
			$layout .= "<td><a title='Edit Page' href='$PHP_SELF?n=pages/pages&a=3&d={$page['id']}'><img alt='Edit Page' border='0' src='templates/$template/images/buttons/edit.gif' /></a> <a title='Delete Page' href='$PHP_SELF?n=pages/pages&a=5&d={$page['id']}'><img alt='Delete Page' border='0' src='templates/$template/images/buttons/delete.gif' /></a></td>";
			$layout .= "</tr>";
			}
		}else{
			$layout .= "<tr class='con1'><td colspan='6'>No Pages Added</td></tr>";
		}

		//-----------------------------------------------------------------
		//  Show Layout
		//-----------------------------------------------------------------	
		echo"<table class='main' cellpadding='4' cellspacing='1'>";
		echo"<tr class='head'>";
		echo"<td colspan='6'>Pages</td>";
		echo"</tr>";
		echo"<tr class='bottom'>";
		echo"<td width='1%'>#</td>";
		echo"<td width='20%'>Page</td>";
		echo"<td width='20%'>Added By</td>";
		echo"<td width='20%'>Access Level</td>";
		echo"<td width='29%'>Date</td>";
		echo"<td width='10%'></td>";
		echo"</tr>";		
		echo"$layout";
		echo"</table>";

		echo"<br />";
		
		//-----------------------------------------------------------------
		//  Get Access Levels
		//-----------------------------------------------------------------	
		$selectOptions = array('All','Registered Members','Administrators');
		
		foreach($selectOptions as $selectOption){
			
			if($selectOption == "All"){ $value = "all"; }
			if($selectOption == "Registered Members"){ $value = "registered"; }
			if($selectOption == "Administrators"){ $value = "admin"; }
			
			$selectBox .= "<option value='$value'>$selectOption</option>";
			
			$value = NULL;
		}
		
		if($_GET['error'] == 1) $status = " | <font color='red'>Name Cannot Be Blank</font>";
		if($_GET['error'] == 2) $status = " | <font color='red'>Access Name Cannot Be Blank</font>";
		if($_GET['error'] == 3) $status = " | <font color='red'>Access Name Already In Use</font>";
		if($_GET['error'] == 4) $status = " | <font color='red'>Page Content Cannot Be Blank</font>";
		if(!isset($_GET['error'])) $status = NULL;

		//-----------------------------------------------------------------
		//  Add Page Form
		//-----------------------------------------------------------------	
		echo"<form name='addPage' method='POST' action='$PHP_SELF?n=pages/pages&a=2'>";
		echo"<table class='main' cellpadding='4' cellspacing='1'>";
		echo"<tr class='head'>";
		echo"<td colspan='5'>Add Pages$status</td>";
		echo"</tr>";
		echo"<tr class='bottom'>";
		echo"<td width='30%'>Field Names</td>";
		echo"<td>Fields</td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Name: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='name' /></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td valign='top'>Access Name: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='shortname' /><br /><font color='red'>No special charactors nor spaces.</font></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Access Level: </td>";
		echo"<td><select class='box' style='width:80%;' name='type'>$selectBox</select></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Idioma: </td>";
		echo"<td><input type='text' class='box' style='width:10%;' name='language' /></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td valign='top'>Page Content: </td>";
		echo"<td><textarea rows='10' class='box' style='width:80%;' name='content'></textarea></td>";
		echo"</tr>";		
		echo"<tr class='con1'>";
		echo"<td colspan='2' align='right'><input type='submit' style='width:120' class='box' value='Add Page'></td>";
		echo"</tr>";
		echo"</table>";	
		echo"</form>";	
	break;

	//-----------------------------------------------------------------
	//  Add Page Process
	//-----------------------------------------------------------------	
	case 2:
		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }

		//-----------------------------------------------------------------
		//  Checks
		//-----------------------------------------------------------------	
		if(!$_POST['name']) { header("location: $PHP_SELF?n=pages/pages&a=1&error=1"); exit;}
		if(!$_POST['shortname']) { header("location: $PHP_SELF?n=pages/pages&a=1&error=2"); exit;}
		if(!$_POST['content']) { header("location: $PHP_SELF?n=pages/pages&a=1&error=4"); exit;}
		
		//-----------------------------------------------------------------
		//  Strip Slashes
		//-----------------------------------------------------------------
		$shortname = jcm_htmlspecialchars(jcm_stripslashes($_POST['shortname']));
		$name = jcm_htmlspecialchars(jcm_stripslashes($_POST['name']));
		$content = jcm_htmlspecialchars(jcm_stripslashes($_POST['content']));

		//-----------------------------------------------------------------
		//  Do Some Checks
		//-----------------------------------------------------------------
		$page = $db->getLine("SELECT * FROM page WHERE shortname = '$shortname'");
		if($page == TRUE) { header("location: $PHP_SELF?n=pages/pages&a=1&error=3"); exit;}
		
		//-----------------------------------------------------------------
		//  Add To Database If Okay
		//-----------------------------------------------------------------		
		
		$info['name'] = $name;
		$info['shortname'] = $shortname;
		$info['content'] = $content;
		$info['type'] = $_POST['type'];
		$info['user_name'] = $_SESSION['username'];
		$info['user_id'] = $_SESSION['id'];
		$info['date'] = time();
		$info['language'] = $language;		
		
		$db->insertRow("page",$info);

		//-----------------------------------------------------------------
		//  Redirect
		//-----------------------------------------------------------------				
		
		header("location: $PHP_SELF?n=pages/pages&d=$shortname");
	break;

	//-----------------------------------------------------------------
	//  Edit Page Form
	//-----------------------------------------------------------------	
	case 3:
		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }
		//-----------------------------------------------------------------
		// Get Page From DB
		//-----------------------------------------------------------------
		$page = $db->getLine("SELECT * FROM page WHERE id = '{$_GET['d']}'");
		if($page == FALSE) { header("location: $PHP_SELF?i=error"); }	

		//-----------------------------------------------------------------
		//  Get Access Levels
		//-----------------------------------------------------------------	
		$selectOptions = array('All','Registered Members','Administrators');
		
		foreach($selectOptions as $selectOption){
			$selected1 = NULL;
			$selected2 = NULL;
			$selected3 = NULL;		
			
			if($selectOption == "All"){ $value = "all";}
			if($selectOption == "Registered Members"){ $value = "registered"; }
			if($selectOption == "Administrators"){ $value = "admin"; }
			
			$selectBox .= "<option value='$value'>$selectOption</option>";
			
			$value = NULL;
		}
		
		if($_GET['error'] == 1) $status = " | <font color='red'>Name Cannot Be Blank</font>";
		if($_GET['error'] == 2) $status = " | <font color='red'>Access Name Cannot Be Blank</font>";
		if($_GET['error'] == 3) $status = " | <font color='red'>Access Name Already In Use</font>";
		if($_GET['error'] == 4) $status = " | <font color='red'>Page Content Cannot Be Blank</font>";
		if($_GET['error'] == 5) $status = " | <font color='red'>Username Cannot Be Blank</font>";
		if($_GET['error'] == 6) $status = " | <font color='red'>User ID Content Cannot Be Blank</font>";
		if(!isset($_GET['error'])) $status = NULL;

		//-----------------------------------------------------------------
		//  Add Page Form
		//-----------------------------------------------------------------	
		echo"<form name='addPage' method='POST' action='$PHP_SELF?n=pages/pages&a=4&d={$_GET['d']}'>";
		echo"<table class='main' cellpadding='4' cellspacing='1'>";
		echo"<tr class='head'>";
		echo"<td colspan='5'>Edit Pages$status</td>";
		echo"</tr>";
		echo"<tr class='bottom'>";
		echo"<td colspan='5'>UserDetails</td>";
		echo"</tr>";				
		echo"<tr class='con1'>";
		echo"<td>Username: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='user_name' value='{$page['user_name']}' /></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>User ID: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='user_id' value='{$page['user_id']}' /></td>";
		echo"</tr>";		
		echo"<tr class='bottom'>";
		echo"<td colspan='5'>Page Details</td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Name: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='name' value='{$page['name']}' /></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td valign='top'>Access Name: </td>";
		echo"<td><input type='text' class='box' style='width:80%;' name='shortname' value='{$page['shortname']}' /><br /><font color='red'>No special charactors nor spaces.</font></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Access Level: </td>";
		echo"<td><select class='box' style='width:80%;' name='type'>$selectBox</select></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Idioma: </td>";
		echo"<td><input type='text' class='box' style='width:10%;' name='language' /></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td valign='top'>Page Content: </td>";
		echo"<td><textarea rows='40' class='box' style='width:100%;' name='content'>{$page['content']}</textarea></td>";
		echo"</tr>";		
		echo"<tr class='con1'>";
		echo"<td colspan='2' align='right'><input type='submit' style='width:120' class='box' value='Edit Page'></td>";
		echo"</tr>";
		echo"</table>";	
		echo"</form>";	
	
	break;
	
	//-----------------------------------------------------------------
	//  Edit Page Process
	//-----------------------------------------------------------------	
	case 4:
		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }
		//-----------------------------------------------------------------
		//  Do Some Checks
		//-----------------------------------------------------------------
		if(!$_POST['name']) { header("location: $PHP_SELF?n=pages/pages&a=3&error=1&d={$_GET['d']}"); exit;}
		if(!$_POST['shortname']) { header("location: $PHP_SELF?n=pages/pages&a=3&error=2&d={$_GET['d']}"); exit;}
		if(!$_POST['content']) { header("location: $PHP_SELF?n=pages/pages&a=3&error=4&d={$_GET['d']}"); exit;}
		if(!$_POST['user_name']) { header("location: $PHP_SELF?n=pages/pages&a=3&error=5&d={$_GET['d']}"); exit;}
		if(!$_POST['user_id']) { header("location: $PHP_SELF?n=pages/pages&a=3&error=6&d={$_GET['d']}"); exit;}
		
		//-----------------------------------------------------------------
		//  Do Some MORE Checks
		//-----------------------------------------------------------------
		$page = $db->getLine("SELECT * FROM page WHERE shortname = '$shortname' AND id != '{$_GET['d']}'");
		if($page == TRUE) { header("location: $PHP_SELF?n=pages/pages&a=3&error=3&d={$_GET['d']}"); exit;}		
	
		//-----------------------------------------------------------------
		//  Add To Database If Okay
		//-----------------------------------------------------------------		
		
		$info['name'] = $name;
		$info['shortname'] = $shortname;
		$info['content'] = $content;
		$info['type'] = $_POST['type'];
		$info['user_name'] = $user_name;
		$info['user_id'] = $user_id;	
		
		$db->updateRow("page",$info,"id={$_GET['d']}");	
		
		header("location: $PHP_SELF?n=pages/pages&d={$info['shortname']}");
	break;
	
	//-----------------------------------------------------------------
	//  Delete Page
	//-----------------------------------------------------------------	
	case 5:
		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }	
	//-----------------------------------------------------------------
	// Get Page From DB
	//-----------------------------------------------------------------
	$page = $db->getLine("SELECT * FROM page WHERE id = '{$_GET['d']}'");
	if($page == FALSE) { header("location: $PHP_SELF?i=error"); }

	if(!isset($_POST['delete'])){
		echo"<form action='$PHP_SELF?n=pages/pages&a=5&d={$_GET['d']}' method='POST'>";
		echo"<table class='main' cellpadding='4' cellspacing='1'>";
		echo"<tr class='head'>";
		echo"<td><font color='red'>Confirm Delete</font></td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>Are you sure you wish to delete `{$page['name']}`?</td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td align='center'><input type='submit' class='box' name='delete' value='Delete Page' /></td>";
		echo"</tr>";	
		echo"</table>";
		echo"</form>";
	}else{
	
		$db->deleteRow("page","id = '{$_GET['d']}'");
		header("location: $PHP_SELF?n=pages/pages&a=1");
	}

	break;	

	//-----------------------------------------------------------------
	//  Show Page
	//-----------------------------------------------------------------	
	default:
		//-----------------------------------------------------------------
		// Redirect If Admin Is Not Looking At Page
		//-----------------------------------------------------------------
		if(!isset($_GET['d']) && $_SESSION['authorization'] >= 3) { header("location: $PHP_SELF?n=pages/pages&a=1"); exit;}
		
		//-----------------------------------------------------------------
		// Get Page From DB
		//-----------------------------------------------------------------
		$page = $db->getLine("SELECT * FROM page WHERE shortname = '{$_GET['d']}'");
		if($page == FALSE) { header("location: $PHP_SELF?i=error"); }
		
		//-----------------------------------------------------------------
		// Filter Access Rights
		//-----------------------------------------------------------------
		if($page['type'] == "admin" && $_SESSION['authorization'] <= 2) { header("location: $PHP_SELF?i=error"); }
		if($page['type'] == "registered" && !isset($is_loged_in)) { header("location: $PHP_SELF?i=error"); }
		
		//-----------------------------------------------------------------
		// Show Layout
		//-----------------------------------------------------------------	
		echo"<table class='main' cellpadding='4' cellspacing='1'>";
		echo"<tr class='head'>";
		echo"<td>{$page['name']}</td>";
		echo"</tr>";
		echo"<tr class='con1'>";
		echo"<td>".parse_message($page['content'])."</td>";
		echo"</tr>";		
		echo"</table>";
		
	break;	
}
?>
